import javax.microedition.lcdui.*;
/**
 * @author Alejo
 */
public class CanvasForm extends Canvas
{
    private int px = (getWidth()/20)*10;
    private int py = (getHeight()/20)*10;
    private int yr = 10;
    private int xr = 10;
    //matriz de prueba
    private int [][] mapmatrix ;
    
     

      /**
     * constructor
     */
        
    protected CanvasForm()
    {
        this.setFullScreenMode(false);
        // matriz de prueba
        mapmatrix = new int [21][21];
          for (int i = 0; i<20;i++){
        for (int j = 0; j <20; j ++){ 
              mapmatrix[i][j]= 0;
            }
         }
        mapmatrix[yr][xr] = 2;
    }
       public void setPx(int px){
        this.px = px;
    }
       public void setPy(int py){
        this.py = py;
    }
       public void setXr(int xr){
        this.xr = xr;
    }
       public void setYr(int yr){
        this.yr = yr;
    }
      public void setMapmatrix(int [][] mapmatrix){
        this.mapmatrix = mapmatrix;
    }
       public int getPx(){
        return px;
    }
       public int getPy(){
        return py;
    }
        public int getYr(){
        return yr;
    }
         public int getXr(){
        return xr;
    }
          public int [][] getMapmatrix(){
        return mapmatrix;
    }
         public void movAbajo(){
              if(py < (getHeight()/20)*19){
                  if(yr < 20){
                   if (mapmatrix[yr + 1][xr] != 1){
                        py = py + getHeight()/20;
                        if (mapmatrix[yr][xr]!= 1)
                            mapmatrix[yr][xr] = 3;
                        yr ++;
                        mapmatrix[yr][xr] = 2;
                   }
                  }
                }
              }
          public void movArriba(){
          if(py > 0){
                    if(yr > 1  ){
                    if (mapmatrix[yr -1][xr] != 1){
                    py = py -getHeight()/20;
                     if (mapmatrix[yr][xr]!= 1)
                            mapmatrix[yr][xr] = 3;
                    yr --;
                     mapmatrix[yr][xr] = 2;
                    }}
                }
          }
          public void movDerecha(){
          if(px < (getWidth()/20)*19){
                    if( xr <19 ){
                    if (mapmatrix[yr][xr+1] != 1){
                    px = px + getWidth()/20;
                     if (mapmatrix[yr][xr]!= 1)
                        mapmatrix[yr][xr] = 3;
                    xr ++;
                    mapmatrix[yr][xr] = 2;
                    }
                    }
                }
          }
          
          public void movIzquierda(){
           if(px > 0){
                    if(xr>0 ){
                    if (mapmatrix[yr][xr-1] != 1 ){
                    px = px - getWidth()/20; 
                     if (mapmatrix[yr][xr]!= 1)
                        mapmatrix[yr][xr] = 3;
                    xr --;
                    mapmatrix[yr][xr] = 2;
                    }
                    }
                }
          }
          
          
          /**
//     * paint
//     */
    
    
    
      protected void paint(Graphics g) 
      {
          int defcolor = 0;
          int width = getWidth()/20;
          int height = getHeight()/20;
                  
          g.setColor(0, 0, 0);
          g.fillRect(0, 0, width*20, height*20);        
    //inicio del for matrix
     
    for (int f = 0; f<21;f++){
        for (int c = 0; c <20; c ++){
            defcolor = mapmatrix[f][c];
       switch(defcolor)
         {    
            case 0: // nada fondo
                g.setColor(0, 0, 0);
                g.fillRect(width*c, height*(f-1), width, height);
                break;
            case 1: //pared
                g.setColor(40, 100, 50);
                g.fillRect(width*c, height*(f-1), width, height);
                break;
            case 2: // robot
             px = width*c;
             py = height*(f-1);
             g.setColor(255,0,0);
             g.fillRect(px, py, width, height);
                break;
            case 3: // migajas de pan
                g.setColor(255, 255, 255);
              g.fillArc(width*c + width/2 , height*(f-1) + height/2, width/4, height/4,0 ,360 );  
                break;
         }
        }
    }
       // fin del for matrix   
   for(int h = 1; h < 21; h ++){
       for (int w = 1; w < 21; w ++){
          g.setColor(255, 128, 0);
          g.drawRect(0, 0,w*width,h*height);
       }
   } 
   
   // llamar aqui a genmap
   
                 
      }
      
    /**
     * Called when a key is pressed.
     */
      protected void keyPressed(int keyC)
     {
         super.keyPressed(keyC);
         switch(keyC)
         {    
            case Canvas.KEY_NUM8:
               movAbajo();
                break;
            case Canvas.KEY_NUM2:
               movArriba();
                break;
            case Canvas.KEY_NUM6:
               movDerecha();
                break;
            case Canvas.KEY_NUM4:
                movIzquierda();
                break;
         }
         this.repaint();
     }     
     
   
    
    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyC) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyC) {}

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {}

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {}

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {}

    /**
     * Called when action should be handled
     */
    public void commandAction(Command c, Displayable d) {}
}